<?php
//session_start();
include("chk_permission.php");//must to be member
//check permission -------------------------
if($user_type <> "2" && $user_type <> "3"  && $user_type <> "4"  && $user_type <> "5"){// admin and director only
	?>
		<script type="text/javascript">
			window.location.href='index.php';
		</script>
	<?
		exit();
}
// -----------------------------------------
$user_email = $_SESSION["sess_email"];
?>
<script language="javascript">
function checkInput(){
		if(document.form1.email.value ==""){
   				alert("กรุณาป้อนอีเมล์");
   				document.form1.email.focus();
   				return false;
		}else if(document.form1.pwd.value == ""){
   				alert("กรุณาป้อนรหัสผ่าน");
   				document.form1.pwd.focus();
   				return false;
	 	}else if(document.form1.fullname.value ==""){
   				alert("กรุณาป้อนชื่อ-นามสกุล");
   				document.form1.fullname.focus();
   				return false;
		}else if(document.form1.pid.value =="" ||  document.form1.pid.value.length != 13){
   				alert("กรุณาป้อนเลขประจำตัวประชาชนให้ครบถ้้วน");
   				document.form1.pid.focus();
   				return false;
		}else if(document.form1.user_type.value ==""){
   				alert("กรุณาเลือกประเภทผู้ใช้");
   				document.form1.user_type.focus();
   				return false;
		}else{
   				return true;
		}
	}
	
	
	function check_num(e)
	{
   		var keyPressed;
   		if(window.event){
      		keyPressed = window.event.keyCode; // IE
       		if ((keyPressed < 45) || (keyPressed > 57)){
			 alert("ป้อนได้เฉพาะตัวเลขเท่านั้น");
			 window.event.returnValue = false;
			}
   		}else{
      		keyPressed = e.which; // Firefox      
       		if ((keyPressed < 45) || (keyPressed > 57)){
			 alert("ป้อนได้เฉพาะตัวเลขเท่านั้น");
			 keyPressed = e.preventDefault();
			}
    	}
	}


   function changepwd(){
    var email=document.getElementById("email").value;
    var pwd=document.getElementById("pwd").value;
    var newpwd=document.getElementById("newpwd").value;
    var newpwd2=document.getElementById("newpwd2").value;
	if((newpwd!=newpwd2) || (newpwd=="")){
		alert("ยืนยันรหัสผ่านไม่ถูกต้อง");
	}else{
		var urlstr="chngpwd2.php";
		if (window.XMLHttpRequest) {
   			request = new XMLHttpRequest();
		}
			else if (window.ActiveXObject) {
   			request = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		pmeters="email="+email+"&pwd="+pwd+"&newpwd="+newpwd+"&newpwd2="+newpwd2;

		request.open('POST', urlstr , true); // request XML from PHP with AJAX call          
		request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");           
		request.setRequestHeader("Content-length", pmeters.length);            
		request.setRequestHeader("Connection", "close"); 
	
		request.send(pmeters); 
		request.onreadystatechange = function () { // if function
		//alert("wait");
		//alert(request.readyState);
		if (request.readyState == 4) {//if readyState == 4
			var textResponse=request.responseText;
			var arr_response = textResponse.split(':');
			if(arr_response[0]=="1"){
			   	alert(arr_response[1]);
			   	document.location = "index.php?tab=0";
			}else{
				alert(arr_response[1]);
			}
		}// end if readyState == 4
		}// end function waiting
		request.send(null); 
   	}//if newpwd = newpwd2
}// 
</script>
<form method="post" action="changepw2.php">
<br />
  <table width="400" border="0" align="center" cellspacing="0">
  <tr>
    <td height="35"><b><font size="2">อีเมล์</font></b></td>
    <td height="35"><font size="2"><?=$user_email?>
      <input type="hidden" name="email" id="email"  value="<?=$user_email?>"/>
    </font></td>
  </tr>
  <tr>
    <td height="35"><font size="2"><B>รหัสผ่านเดิม</B></font></td>
    <td height="35"><font size="2">
      <INPUT name="pwd" type="password" class="textbox_gray2" id="pwd">
      <font color="#FF0000">*</font></font></td>
  </tr>
  <tr>
    <td height="35"><font size="2"><B>รหัสผ่านใหม่</B></font></td>
    <td height="35"><font size="2">
      <INPUT name="newpwd" type="password" class="textbox_gray2" id="newpwd">
      <font color="#FF0000">*</font></font></td>
  </tr>
  <tr>
    <td height="35"><font size="2"><B>ยืนยันรหัสผ่านใหม่อีกครั้ง</B></font></td>
    <td height="35"><font size="2">
      <INPUT name="newpwd2" type="password" class="textbox_gray2" id="newpwd2">
      <font color="#FF0000">*</font></font></td>
  </tr>
  <tr>
    <td height="35" colspan="2" align="center"><font size="2">
      <INPUT name="Submit" TYPE="button" class="button_gray" VALUE="ตกลง" onclick="changepwd();">
      <input name="Reset" type="Reset" class="button_gray" value="ยกเลิก">
    </font></td>
  </tr>
</table>
</form>
